package com.verisette.ncos.service.card.bizunit;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.corp.agi.common.exception.ProcessException;
import org.corp.agi.service.bean.ProcessContext;
import org.corp.agi.service.bizunit.CommonBusinessUnit;

import com.verisette.ncos.persistence.domain.MasterMember;
import com.verisette.ncos.persistence.domain.MasterUserProfile;
import com.verisette.ncos.web.card.bean.CardCreateBean;
import com.verisette.ncos.web.utilities.SendNotifyMailThread;

public class NotifyNormalUserByEmailBU extends CommonBusinessUnit {
//	private Logger logger = Logger.getLogger(NotifyNormalUserByEmailBU.class);
	
	@Override
	public void execute(ProcessContext processContext) throws ProcessException {
		// TODO Auto-generated method stub
		
		MasterUserProfile sender = (MasterUserProfile)processContext.getUserBean();
		
		CardCreateBean cardCreateBean = (CardCreateBean)processContext.getOriginalRequest().getValueObject();
		SendNotifyMailThread sendMailThread = new SendNotifyMailThread();
		sendMailThread.setMailTemplate("mail-approve-request");
		sendMailThread.setOrder(cardCreateBean.getOrder());
		sendMailThread.setSender(sender);
		
		MasterUserProfile receiver = (MasterUserProfile) commonDAO.findById(MasterUserProfile.class, sender.getApproverUspId());
		sendMailThread.setReceiver(receiver);
		
		MasterMember member = cardCreateBean.getOrder().getMasterMember();
		String requesterName = member.getMmbFnameTh(), requesterLname = member.getMmbLnameTh();
		if( StringUtils.isBlank(requesterName)){
			requesterName = member.getMmbFnameEn();
			requesterLname = member.getMmbLnameEn();
		}
		
		String name = "ไม่ทราบชื่อ";
		if(StringUtils.isNotBlank(receiver.getFirstName())){
			name = receiver.getFirstName();
			if(StringUtils.isNotBlank(receiver.getLastName())){
				name = name + " " + receiver.getLastName();
			}
		}
		
		sendMailThread.addContentParam(name);
		StringBuilder sbf = new StringBuilder(sender.getFirstName());
		sbf.append(" ").append(sender.getLastName());
		sbf.append(" (").append(requesterName);
		sbf.append(" ").append(requesterLname);
		sendMailThread.addContentParam(sbf.toString());
		sendMailThread.addContentParam("<a href=\"http://localhost:8080/ncos\">ระบบนามบัตรออนไลน์</a>");
		
		sendMailThread.start();
	}
}